﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Entities.IRepository;
using Entities;
using BLToolkit.Data;
using BLToolkit.Data.Linq;

namespace DAL.Admin
{
    public class BLTDiaChiRepositoy : ConnectionStringBL,IDiaChiRepository
    {
      //DbManager db = new DbManager("QuanLiBongDa");
        public bool ThemDiaChi(DiaChi dc)
        {
            try
            {
               int kq= db.Insert(dc);
                db.CommitTransaction();
                return kq>0;
            }
            catch (Exception)
            {
             db.RollbackTransaction();
                return false;
                 
            }
        }

        public bool CapNhatDiaChi(DiaChi dc)
        {
             try
            {
               int kq= db.Update(dc);
                db.CommitTransaction();
                return kq>0;
            }
            catch (Exception)
            {
             db.RollbackTransaction();
                return false;
                 
            }
        }

        public bool XoaDiaChi(DiaChi dc)
        {
           try
            {
               int kq= db.Delete(dc);
                db.CommitTransaction();
                return kq>0;
            }
            catch (Exception)
            {
             db.RollbackTransaction();
                return false;
                 
            }
        }

        public DiaChi LayTheoId(string id)
        {
            return db.GetTable<DiaChi>().SingleOrDefault(x=>x.MaDiaChi==id);
        }

        public DiaChi LayChiTietTheoId(string id)
        {
           // DiaChi dc=LayTheoId(id);
           //if(dc!=null)
           //{
           //    dc.DsNhanVien=db.GetTable<NhanVien>().Where(x=>x.MaDiaChi==id)
           //        .ToList();
           //}
           // return dc;
            return null;
        }

        public List<DiaChi> LayTatCa()
        {
            return db.GetTable<DiaChi>().ToList();

        }


        public List<DiaChi> LayTinhTP(string tinhtp)
        {
            return db.GetTable<DiaChi>().Where(x => x.TinhTP == tinhtp)
                .ToList();
        }

        public List<DiaChi> LayQuanHuyen(string quanhuyen)
        {
            return db.GetTable<DiaChi>().Where(x => x.QuanHuyen == quanhuyen)
             .ToList();
        }

        public List<DiaChi> LayTenDuong(string tenduong)
        {
            return db.GetTable<DiaChi>().Where(x => x.TenDuong == tenduong)
             .ToList();
        }
    }
}
